package com.shengqi.xinnongren.common.config.network;

import android.text.TextUtils;

import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.AppUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.work.MyApplication;
import com.shengqi.xinnongren.work.ui.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;
import java.util.Set;

/**
 * 常量类
 */
public class Constants {
    public static final String CLIENT = "android";
    public static final String PACKAGE = AppUtils.getPackageName(MyApplication.getContext());
    public static final String VER = AppUtils.getVersionName(MyApplication.getContext());
    public static final int ABOUT_US = 1;
    public static final int PRODUCT_DES = 2;
    public static final int BUY_KNOW = 3;
    public static final int VERSION_INTRO = 4;
    public static final int REGISTER_PROTOCOL = 5;
    public static final int USER_PROTOCOL = 7;


    public static JSONObject getJsonData(Map<String, Object> maps, String token) {

        JSONObject params = new JSONObject();
        try {
            params.put("package", Constants.PACKAGE);
            params.put("client", Constants.CLIENT);
            params.put("version", Constants.VER);
            params.put("isaes", 0);

            if (!TextUtils.isEmpty(token)) {
                params.put("token", token);
            } else {
                params.put("token", "");
            }

            if (maps != null) {
                JSONObject data = new JSONObject();
                Set<Map.Entry<String, Object>> entries = maps.entrySet();
                for (Map.Entry<String, Object> entry : entries) {
                    data.put(entry.getKey(), entry.getValue());
                }
                params.put("data", data);
            } else
                params.put("data", "");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
        LogUtil.e("调用接口（JK）", String.format(" \n 方法：%s.%s()\n 发送给服务器的参数 :\n%s", stackTraceElement.getClassName(), stackTraceElement.getMethodName(), JsonUtil.formatJson(params.toString())));
        return params;
    }

    public static JSONObject getAESJsonData(Map<String, Object> maps, String token) {

        JSONObject params = new JSONObject();
        try {
            params.put("package", Constants.PACKAGE);
            params.put("client", Constants.CLIENT);
            params.put("version", Constants.VER);
            params.put("isaes", 1);
            if (!TextUtils.isEmpty(token)) {
                params.put("token", token);
            }

            if (maps != null) {
                JSONObject data = new JSONObject();
                Set<Map.Entry<String, Object>> entries = maps.entrySet();
                for (Map.Entry<String, Object> entry : entries) {
                    data.put(entry.getKey(), entry.getValue());
                }
                StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
                LogUtil.e("调用接口（JK）", String.format(" \n 方法：%s.%s()\n 发送给服务器的参数 :\n%s", stackTraceElement.getClassName(), stackTraceElement.getMethodName(), JsonUtil.formatJson(data.toString())));
                params.put("data", SafeUtils.encrypt(data.toString()));
            } else
                params.put("data", "");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        LogUtil.e("调用接口（JK）密文:" + params.toString());
        return params;
    }
}
